package cn.hicard.auth.web;

import java.util.List;

/**
 * 表示权限资源的树形结构，
 * @author zhangy
 *
 */
public class Node {	
	
	/** 节点类型：导航，可以包含子节点 */
	public static String TYPE_NAVI = "1";
	/** 节点类型：控件，不能拥有子节点 */
	public static String TYPE_CONTROL = "2";
	
	/**
	 * 重写的等于判断，目前只需简单判断主键值相等即可
	 * @see java.lang.Object
	 */
	@Override
	public boolean equals(Object obj) {
		if(obj == null){
			return false;
		}
		if(obj instanceof Node){
			return this.pk ==  ((Node)obj).pk;
		}else{
			return false;
		}
	}
	
	/**
	 * 列出将当前节点及所有层级节点放入集合nodes中<br/>
	 * 备注：2013-5-3 感谢某匿名网友的优化
	 */
	public void listNode(List<Node> nodes){
		nodes.add(this);
		if(children != null && children.size()>0){
			for (Node node : children) {
				node.listNode(nodes);
			}
		}
	}
	
	
//	/**
//	 * 根据指定的资源路径查找节点
//	 * @param path 从当前节点开始到目标结点，由controlid组成的路径
//	 * @return
//	 */
//	public Node findByPath(String[] idurl){
//		
//		if(idurl == null || idurl.length==0) return null;
//
//		Node child = null;	
//		
//		for (String id : idurl) {
//			child = child.findChild(id);
//		}
//		
//		return child;
//	}
	
	
	/**
	 * 遍历node，查找与指定Pk相同的节点
	 * @param node
	 * @param pk
	 * @return
	 */
	public Node findByPk(int targetPk){
		
		if(this.pk == targetPk) return this;

		Node child = null;				
		
		if(children != null && children.size()>0){
			for (Node nd : children) {
				child = nd.findByPk(targetPk);
				if(child != null) break;
			}
		}
		
		return child;
	}
	
//	public static void main(String[] args) {
//		
//		Node root = create(1,"menu","菜单根");
//		Node l1 = create(2,"newreck","newreck");l1.setParent(root);
//		Node l2 = create(3,"merch","merch");l2.setParent(root);
//		root.children = new ArrayList<Node>();
//		root.children.add(l1);
//		root.children.add(l2);
//		Node l1a = create(4,"basedata","basedata");l1a.setParent(l1);
//		Node l1b = create(5,"compdata","compdata");l1b.setParent(l1);
//		
//		Node l1a1 = create(6,"merch","merch");l1a1.setParent(l1a);
//		Node l1a2 = create(7,"fee","fee");l1a2.setParent(l1a);
//		Node l1a3 = create(8,"hmerch","hmerch");l1a3.setParent(l1a);
//		
//		Node node = root.findByPath(new String[]{"merch","basedata","merch"});
//		System.out.println(node.getName());
//	}
	
	public static Node create(int pk,String ctrlId,String name){
		Node node = new Node();
		node.setControlid(ctrlId);
		node.setName(name);
		node.setPk(pk);		
		return node;
	}
	
	/**
	 * 跟据资源ID查找当前及当前节点下的子节点，注意，在当前层级下资源ID是唯一的
	 * @param sourceId
	 * @return
	 */ 
	public Node findChild(String sourceId){
		if(this.controlid.equals(sourceId)){
			return this;
		}
		if(children == null || children.size()==0){
			return null;
		}else{
			for (int i = 0; i < children.size(); i++) {
				Node node = children.get(i);				
				if(sourceId.equals(node.controlid)){
					return node;
				}				
			}
			return null;
		}
	}
		
	// 主键，全局唯一编号
	
	private int pk;
	// 名称
	private String name;
	// ID，在当前层级中的唯一编号
	private String controlid;
	// 等级，当前所处的深度，最顶级为0，暂未实现
	private int level;
	// 子集合
	private List<Node> children;
	// 上级
	private Node parent;
	
	public int getPk() {
		return pk;
	}


	public void setPk(int pk) {
		this.pk = pk;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getControlid() {
		return controlid;
	}


	public void setControlid(String controlid) {
		this.controlid = controlid;
	}


	public int getLevel() {
		return level;
	}


	public void setLevel(int level) {
		this.level = level;
	}


	public List<Node> getChildren() {
		return children;
	}


	public void setChildren(List<Node> children) {
		this.children = children;
	}


	public Node getParent() {
		return parent;
	}


	public void setParent(Node parent) {
		this.parent = parent;
	}
}
